package com.brsya.movie.util;

import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.brsya.base.base.Contents;
import com.brsya.base.bean.MovieBean;
import com.brsya.base.util.GlideUtil;
import com.brsya.base.util.StringUtil;
import com.brsya.base.util.UiUtil;
import com.brsya.movie.R;
import com.brsya.movie.activity.InventoryDetailActivity;

/**
 * Created by Brsya
 * CreateDate: 2022/3/25  10:31
 * Description:
 * app专用Util
 */
public class AppUtil {

    /**
     * 启动到应用商店app详情界面
     *
     * @param appPkg    目标App的包名
     * @param marketPkg 应用商店包名 ,如果为""则由系统弹出应用商店列表供用户选择,否则调转到目标市场的应用详情界面，某些应用商店可能会失败
     */
    public static boolean launchAppDetail(Context context, String appPkg, String marketPkg) {
        try {
            if (TextUtils.isEmpty(appPkg)) return false;

            Uri uri = Uri.parse("market://details?id=" + appPkg);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            if (!TextUtils.isEmpty(marketPkg)) {
                intent.setPackage(marketPkg);
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 打开片单详情页
     *
     * @param context 上下文
     * @param id      标题id
     * @param name    标题
     * @param imgUrl  图片地址
     */
    public static void openInventoryDetail(Context context, int id, String name, String imgUrl) {
        Intent intent = new Intent(context, InventoryDetailActivity.class);
        if (imgUrl == null) {
            intent.putExtra(Contents.INTENT_INVENTORY_NAME, name);
            intent.putExtra(Contents.INTENT_INVENTORY_ID, id);
            context.startActivity(intent);
        }
        GlideUtil.loadBitmapColor(context, imgUrl, new GlideUtil.OnBitmapColorListener() {
            @Override
            public void onBitmapColor(int color) {
                // 图片变暗
                color = color & 0xFFAAAAAA;
                intent.putExtra(Contents.INTENT_INVENTORY_NAME, name);
                intent.putExtra(Contents.INTENT_INVENTORY_ID, id);
                intent.putExtra(Contents.INTENT_INVENTORY_COLOR, color);
                context.startActivity(intent);
            }
        });
    }


    /**
     * LinearLayout中添加标签View
     *
     * @param context 上下文
     * @param box     LinearLayout
     * @param tagsStr 标签字符串
     */
    public static void addTagView(Context context, LinearLayout box, String tagsStr) {
        if (!StringUtil.isEmpty(tagsStr)) {
            String[] tags = tagsStr.split(",");
            for (int i = 0; i < box.getChildCount(); i++) {
                box.getChildAt(i).setVisibility(View.GONE);
            }

            for (int i = 0; i < tags.length; i++) {
                String tag = tags[i];
                if (box.getChildCount() <= i) {
                    box.addView(createTag(context, tag));
                } else {
                    View childAt = box.getChildAt(i);
                    if (childAt instanceof TextView) {
                        TextView tagView = (TextView) childAt;
                        tagView.setText(tag);
                        tagView.setVisibility(View.VISIBLE);
                    }
                }
            }
        }
    }

    /**
     * 创建影视标签View
     *
     * @param text 标签
     * @return TextView
     */
    private static TextView createTag(Context context, String text) {
        TextView tagView = new TextView(context);
        tagView.setText(text);
        tagView.setTextColor(0xFFFFFFFF);
        tagView.setTextSize(10);
        int dp6 = UiUtil.dpToPx(context, 6);
        int dp4 = UiUtil.dpToPx(context, 4);
        tagView.setPadding(dp6, dp4, dp6, dp4);
        tagView.setBackgroundResource(R.drawable.bg_ranking_tag);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.rightMargin = dp6;
        tagView.setLayoutParams(lp);

        return tagView;
    }

    public static void backgroundAnimation(View view, int startColor, int endColor, long duration) {
        //创建动画,这里的关键就是使用ArgbEvaluator, 后面2个参数就是 开始的颜色,和结束的颜色.
        ValueAnimator colorAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), startColor, endColor);
        colorAnimator.addUpdateListener(animation -> {
            int color = (int) animation.getAnimatedValue();//之后就可以得到动画的颜色了
            view.setBackgroundColor(color);//设置一下, 就可以看到效果.
        });
        colorAnimator.setDuration(duration);
        colorAnimator.start();
    }

    public static void textColorAnimation(TextView view, int startColor, int endColor, long duration) {
        //创建动画,这里的关键就是使用ArgbEvaluator, 后面2个参数就是 开始的颜色,和结束的颜色.
        ValueAnimator colorAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), startColor, endColor);
        colorAnimator.addUpdateListener(animation -> {
            int color = (int) animation.getAnimatedValue();//之后就可以得到动画的颜色了
            view.setTextColor(color);//设置一下, 就可以看到效果.
        });
        colorAnimator.setDuration(duration);
        colorAnimator.start();
    }


        /**
         * 拼接影视标签
         *
         * @param movieBean 影视实体类
         * @return 拼接的影视标签
         */
        public static String getMovieTagListStr(MovieBean movieBean) {
            return getMovieTagListStr(movieBean, true);
        }

        /**
         * 拼接影视标签
         *
         * @param movieBean 影视实体类
         * @return 拼接的影视标签
         */
        public static String getMovieTagListStr(MovieBean movieBean, boolean isShowYear) {
            StringBuilder sb = new StringBuilder();
            if (isShowYear) {
                if (!StringUtil.isEmpty(movieBean.getVideoYear())) {
                    sb.append(movieBean.getVideoYear())
                            .append(" / ");
                }
            }
            if (!StringUtil.isEmpty(movieBean.getVideoArea())) {
                sb.append(movieBean.getVideoArea())
                        .append(" / ");
            }
            if (!StringUtil.isEmpty(movieBean.getVideoTag())) {
                sb.append(movieBean.getVideoTag())
                        .append(" / ");
            }
            if (!StringUtil.isEmpty(movieBean.getUpState())) {
                sb.append(movieBean.getUpState())
                        .append(" / ");
            }
            if (sb.length() > 2) {
                sb.delete(sb.length() - 3, sb.length());
            }
            return sb.toString();
        }
}
